package com.mzkj.wanjulaile.mobile.api.modules.activeConfig.controller;


import com.mzkj.common.data.StatusCode;
import com.mzkj.common.utils.Collections3;
import com.mzkj.common.utils.DateUtils;
import com.mzkj.wanjulaile.mobile.api.common.controller.BaseController;
import com.mzkj.wanjulaile.mobile.api.common.entity.ResultEntity;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.Account;
import com.mzkj.wanjulaile.mobile.api.modules.account.service.AccountService;
import com.mzkj.wanjulaile.mobile.api.modules.activeConfig.constant.ActiveConstant;
import com.mzkj.wanjulaile.mobile.api.modules.activeConfig.entity.ActiveShareConfig;
import com.mzkj.wanjulaile.mobile.api.modules.activeConfig.entity.ActiveSharePic;
import com.mzkj.wanjulaile.mobile.api.modules.activeConfig.service.ActiveShareConfigService;
import com.mzkj.wanjulaile.mobile.api.modules.activeConfig.service.ActiveSharePicService;
import com.mzkj.wanjulaile.mobile.api.modules.coupon.entity.AccountCoupon;
import com.mzkj.wanjulaile.mobile.api.modules.coupon.entity.Coupon;
import com.mzkj.wanjulaile.mobile.api.modules.coupon.entity.Invite;
import com.mzkj.wanjulaile.mobile.api.modules.coupon.service.AccountCouponService;
import com.mzkj.wanjulaile.mobile.api.modules.coupon.service.CouponService;
import com.mzkj.wanjulaile.mobile.api.modules.coupon.service.InviteService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分享活动配置Controller
 *
 * @author ianly
 * @version 2017-02-08
 */
@Controller
@RequestMapping(value = "auth/activeConfig")
public class ActiveShareConfigAuthController extends BaseController {
    @Autowired
    private ActiveSharePicService activeSharePicService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private ActiveShareConfigService activeShareConfigService;
    @Autowired
    private InviteService inviteService;
    @Autowired
    private CouponService couponService;
    @Autowired
    private AccountCouponService accountCouponService;

    @RequestMapping(value = "orderShareActive")
    @ResponseBody
    public ResultEntity<ActiveSharePic> orderActiveShare(){
        ResultEntity<ActiveSharePic> result = new ResultEntity<ActiveSharePic>();
        try {
            ActiveSharePic shareRewardPic = activeSharePicService.getByType(ActiveConstant.ORDERD_REWARD_PIC);
            result.setData(shareRewardPic);
            result.setStatusCode(StatusCode.SC0.getValue());
            result.setMsg(StatusCode.SC0.getDesc());
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            result.setStatusCode(StatusCode.SC5000.getValue());
            result.setMsg(StatusCode.SC5000.getDesc());
        }
        return result;
    }

    /**
     * 获得分享奖励
     *
     * @param
     * @return
     */
    @RequestMapping(value = "account/shareReward")
    @ResponseBody
    public ResultEntity<Map<String, Object>> shareReward(HttpServletRequest request) {
        String accountId=(String)request.getAttribute("accountId");
        ResultEntity<Map<String, Object>> result = new ResultEntity<>();
        Map<String, Object> params = new HashMap<>();
        //账号上的分享奖励
        Account account = accountService.getAccount(accountId);
        params.put("totalShareCouponFee", account.getTotalShareCoupon());
        params.put("totalShareTime", account.getTotalShareTime());
        //前台奖励显示flag
        setShowFlag(params);
        //分享出去的数据
        setRewardOutInfo(params, account);
        //获得活动说明图片
        ActiveSharePic shareRewardPic = activeSharePicService.getByType(ActiveConstant.SHARE_REWARD_PIC);
        shareRewardPic.setRemarks(StringEscapeUtils.unescapeHtml4(shareRewardPic.getRemarks()));
        params.put("shareRewardPic", shareRewardPic);
        result.setData(params);
        result.setStatusCode(StatusCode.SC0.getValue());
        result.setMsg(StatusCode.SC0.getDesc());
        return result;
    }

    private void setRewardOutInfo(Map<String, Object> params, Account account) {
        Invite searchInvite = new Invite();
        searchInvite.setInvitedAccount(account);
        //成功邀请充值符合套餐下单
        searchInvite.setSuccessFlag("1");
        List<Invite> inviteList = inviteService.findList(searchInvite);
        //累计数据
        int shareOutTime = 0;
        int shareOutCoupon = 0;
        int shareOutNum = 0;
        params.put("shareOutNum", shareOutNum);
        if (!Collections3.isEmpty(inviteList)) {
            //成功分享给多少人了
            // TODO 2017-03-02 绑定手机也算成功的一种 后期去掉 打开注释
            //params.put("shareOutNum", inviteList.size());
            AccountCoupon accountCoupon = new AccountCoupon();
            for (Invite invite : inviteList) {
                if (invite.getBeInvitedTimeDuration() != null && invite.getBeInvitedTimeUnit() != null) {
                    //累计时长
                    shareOutTime = DateUtils.addTimeWithDurationTimeUnit(shareOutTime, invite.getBeInvitedTimeUnit(), invite.getBeInvitedTimeDuration());
                }
                //累计优惠券
                accountCoupon.setType(ActiveConstant.SHARED_PERSON);
                accountCoupon.setAccount(invite.getBeInvitd());
                List<AccountCoupon> accountCouponList = accountCouponService.newFindList(accountCoupon);
                if (!Collections3.isEmpty(accountCouponList)) {
                    for (AccountCoupon ac : accountCouponList) {
                        shareOutCoupon += (ac.getCount() * Integer.valueOf(ac.getCoupon().getFaceValue()));
                    }
                }
            }
        }
        //绑定手机即显示奖励 (7天 加注册送的10元优惠券)
        searchInvite.setSuccessFlag(null);
        List<Invite> inviteBindList = inviteService.findList(searchInvite);
        if (!Collections3.isEmpty(inviteBindList)){
            //成功分享给多少人了
            // TODO 2017-03-02 绑定手机也算成功的一种 后期去掉 注释
            params.put("shareOutNum", inviteBindList.size());
            for (Invite in : inviteBindList){
                //绑定注册成功后领取会员抵用券
                Coupon coupon = new Coupon();
                coupon.setCouponType(Coupon.COUPON_TYPE_ACCOUNT);
                List<Coupon> couponList = couponService.getCouponByType(coupon);
                if (!Collections3.isEmpty(couponList)) {
                    for (Coupon temp : couponList) {
                        shareOutCoupon += Integer.valueOf(temp.getFaceValue());
                    }
                }
                shareOutTime +=7;
            }
        }


        params.put("shareOutTime", shareOutTime);
        params.put("shareOutCoupon", shareOutCoupon);
    }

    private void setShowFlag(Map<String, Object> params) {
        ActiveShareConfig activeShareConfig = new ActiveShareConfig();
        activeShareConfig.setIsUse("0");
        List<ActiveShareConfig> activeShareConfigList = activeShareConfigService.findList(activeShareConfig);
        if (!Collections3.isEmpty(activeShareConfigList)) {
            ActiveShareConfig shareConfig = Collections3.getFirst(activeShareConfigList);
            boolean sharerTimeFlag = false;
            if (shareConfig.getSharerTimeDuration() != null) {
                //显示分享者获得时长
                sharerTimeFlag = true;
            }
            params.put("sharerTimeFlag", sharerTimeFlag);

            boolean sharerCouponFlag = false;
            if (shareConfig.getSharerCoupon() != null) {
                //显示分享者获得优惠券
                sharerCouponFlag = true;
            }
            params.put("sharerCouponFlag", sharerCouponFlag);

            boolean sharedTimeFlag = false;
            if (shareConfig.getSharedTimeDuration() != null) {
                //显示被分享者获得时长
                sharedTimeFlag = true;
            }
            params.put("sharedTimeFlag", sharedTimeFlag);

            boolean sharedCouponFlag = false;
            if (shareConfig.getSharedCoupon() != null) {
                //显示被分享者获得优惠券
                sharedCouponFlag = true;
            }
            params.put("sharedCouponFlag", sharedCouponFlag);
        }
    }
}